/**
 * Created by awebone on 2017/8/17.
 */

var express=require('express');

var router=express.Router();

var User=require('../models/User');
var Content=require('../models/Content');

//统一返回格式
var responseData;
router.use(function (req,res,next) {
    responseData={
        code:0,
        message:''
    };

    next();
});


/*
* 注册
*   用户逻辑
*   1.用户名不能为空
*   2.密码不能为空
*   3.两次密码必须一致
*
*   1.用户名是否已经注册：数据库查询
*   2.给前端反馈
* */
router.post('/user/register',function (req, res, next) {
    var username=req.body.username;
    var password=req.body.password;
    var repassword=req.body.repassword;

    //用户名不能为空
    if(username===''){
        responseData.code=1;
        responseData.message='用户名不能为空';
        res.json(responseData);
        return;
    }
    //密码不能为空
    if(password===''){
        responseData.code=2;
        responseData.message='密码不能为空';
        res.json(responseData);
        return;
    }
    //两次密码必须一致
    if(password!==repassword){
        responseData.code=3;
        responseData.message='两次密码必须一致';
        res.json(responseData);
        return;
    }

    //用户名是否已经注册，查询数据库是否有相同用户名
    User.findOne({
        username:username
    }).then(function (userInfo) {
        if(userInfo){
            //表示数据库中有该记录
            responseData.code=4;
            responseData.message='用户名已经存在';
            res.json(responseData);
            return;
        }

        //保存用户注册的信息到数据库
        var user=new User({
            username:username,
            password:password
        });
        return user.save();
    }).then(function (newUserInfo) {
        responseData.message='注册成功';
        res.json(responseData);
    });
});

/*
 * 登录
 *   用户逻辑
 *   1.用户名或密码不能为空
 *   2.查询数据库中记录,如果存在则登录成功
 *
 * */
router.post('/user/login',function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;

    if(username===''||password===''){
        responseData.code=1;
        responseData.message='用户名或密码不能为空';
        res.json(responseData);
        return;
    }

    User.findOne({
        username:username,
        password:password
    }).then(function (userInfo) {
        if(!userInfo){
            responseData.code=2;
            responseData.message='用户名或密码错误';
            res.json(responseData);
            return;
        }
        responseData.message='登录成功';
        responseData.userInfo={
            _id:userInfo._id,
            username:userInfo.username
        };

        //发送cookie信息到浏览器
        req.cookies.set('userInfo',JSON.stringify({
            _id:userInfo._id,
            username:userInfo.username
        }));

        res.json(responseData);
        return;
    })
});

/*
 * 退出
 *
 * */
router.get('/user/logout',function (req, res) {
    req.cookies.set('userInfo',null);
    responseData.message='退出成功';
    res.json(responseData);
});


/*
* 获取文章的所有评论
* */
router.get('/comment',function (req, res) {
    var contentId=req.query.contentid || '';

    Content.findOne({
        _id:contentId
    }).then(function (content) {
        responseData.data=content;
        res.json(responseData);
    })
});

/*
*评论提交
* 1.评论人
* 2.评论内容
* 3.评论时间
* * */
router.post('/comment/post',function (req, res) {
    //需要前端传：内容的id
    var contentId=req.body.contentid || '';

    //提交的数据
    var postData={
        username:req.userInfo.username,
        postTime:new Date(),
        content:req.body.content
    };

    //查询当前文章的信息
    Content.findOne({
        _id:contentId
    }).then(function (content) {
        content.comments.push(postData);
        return content.save();
    }).then(function (newContent) {
        responseData.message='评论成功';
        responseData.data=newContent;
        res.json(responseData);
    })
});

module.exports=router;